﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;
using SqEngine;

namespace sqengine.Components
{

    public class PlayerArg : System.EventArgs
    {
        private string message;

        public PlayerArg(string m)
        {
            this.message = m;
        }

        public string Message()
        {
            return message;
        }
    }

    public class PlayerStats
    {

        // delegate declaration
        public delegate void ChangingHandler(object sender, PlayerArg ca);

        // event declaration
        public event ChangingHandler OnDie;
        public event ChangingHandler OnArmorChange;
        public event ChangingHandler OnAmmoEmpty;

        private int _prev_life = 0;
        private int _prev_ammo = 0;

        private int _life = 100;
        private int _max_life = 1000;
        private int _armor = 0;
        private int _max_armor = 1000;

        private int _ammo = 0;
        private int _max_ammo = 100;

        private int _score = 0;

        private WeaponInfo _weapon;

        private Game _game;
       

        private Vector3 _position;
        private string _name;

        public int Life { get { return _life; } set { UpdateLife(value); } }
        public int MaxLife { get { return _max_life; } set { _max_life = value; } }

        public int Armor { get { return _armor; } set { UpdateArmor(value); } }
        public int MaxArmor { get { return _max_armor; } set { _max_armor = value; } }

        public Vector3 Position { get { return _position; } set { _position = value; } }
        public string Name { get { return _name; } set { _name = value; } }


        public int Score { get { return _score; } set { _score = value; } }

        public WeaponInfo Weapon { get { return _weapon; } set { _weapon = value; } }


        public int Ammo { get { return _ammo; } set { UpdateAmmo(value); } }
        public int MaxAmmo { get { return _max_ammo; } set { _max_ammo = value; } }

        public PlayerStats(Game game, int start_life, Vector3 start_position, string name)
        {
            _life = start_life;
            _position = start_position;
            _name = name;
            _game = game;
        }

        private void UpdateLife(int life)
        {
            _prev_life = _life;
            _life = life;


            if (_prev_life != _life)
            {
                ((sqengine)_game).ConsoleSetVar(string.Format(sqengine.VAR_SQLIFE, _name), life);
            }

            if (_life <= 0)
            {
                PlayerArg pa = new PlayerArg("die");
                Die(this, pa);
            }

        }

        private void UpdateArmor(int armor)
        {            
            
            if (_armor != armor)
            {
                ((sqengine)_game).ConsoleSetVar(sqengine.VAR_SQARMOR, _armor);
                PlayerArg pa = new PlayerArg(armor.ToString());                
                ArmorChange(this, pa);
            }

            _armor = armor;
        }

        private void UpdateAmmo(int ammo)
        {
            _prev_ammo = _ammo;
            _ammo = ammo;

            if (_prev_ammo != _ammo)
            {
                ((sqengine)_game).ConsoleSetVar(sqengine.VAR_SQAMMO, _ammo);
            }
            if (_ammo <= 0)
            {
                PlayerArg pa = new PlayerArg("ammo");
                AmmoEmpty(this, pa);

            }
        }

        public void Hit(int value)
        {
            if (_armor != 0)
            {
                UpdateArmor(_armor + value);
            }
            else
            {
                UpdateLife(_life + value);
            }

        }

        void Die(object sender, PlayerArg ca)
        {
            if (OnDie != null)
                OnDie(sender, ca);

        }

        void ArmorChange(object sender, PlayerArg ca)
        {
            if (OnArmorChange != null)
                OnArmorChange(sender, ca);

        }

        void AmmoEmpty(object sender, PlayerArg ca)
        {
            if (OnAmmoEmpty != null)
                OnAmmoEmpty(sender, ca);
        }

    }
}
